package com.quectel.core.module.partybuildingsurvey.dto;

import com.quectel.core.module.partybuildingsurvey.dto.count.PartySurveyPersonRecordEvaluateCount;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:38
 */
@Data
@Schema
public class PartySurveyAnswerDto implements Serializable {

    /**
     * id
     */
    @Schema(description = "id")
    private Long id;

    /**
     * 问卷id
     */
    @Schema(description = "问卷id")
    private Long partySurveyId;

    /**
     * 题目id
     */
    @Schema(description = "题目id")
    private Long questionId;

    /**
     * 题目序号
     */
    @Schema(description = "题目序号")
    private Integer sort;

    /**
     * 内容
     */
    @Schema(description = "内容")
    private String content;


    /**
     * 选项自定义配置
     */
    @Schema(description = "选项自定义配置")
    private String customAttr;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    private Date createTime;

    @Data
    @Schema
    public static class LabelData implements Serializable {
        /**
         * 分数
         */
        @Schema(description = "分数")
        private Double score;

        /**
         * 分数
         */
        @Schema(description = "分数描述")
        private String scoreDesc;

        /**
         * 分数
         */
        @Schema(description = "标签列表")
        private List<String> labelList;
    }

    @Data
    @Schema
    public static class ImageData implements Serializable {
        /**
         * 分数
         */
        @Schema(description = "图片链接")
        private String imgUrl;

        /**
         * 分数
         */
        @Schema(description = "图片描述")
        private String imgDesc;
    }

    @Data
    @Schema
    public static class BlankData implements Serializable {
        /**
         * 最长文本
         */
        @Schema(description = "最长文本")
        private Integer max;
        /**
         * 最短文本
         */
        @Schema(description = "最短文本")
        private Integer min;

        /**
         * 默认显示内容
         */
        @Schema(description = "默认显示内容")
        private String placeholder;
    }

    /************非数据库字段 start ************/
    /**
     * 评分数据列表
     */
    @Schema(description = "评分数据列表")
    private List<LabelData> labelDataList;

    /**
     * 图片数据列表
     */
    @Schema(description = "图片数据列表")
    private ImageData imageData;

    /**
     * 填空对应的配置数据
     */
    @Schema(description = "填空对应的配置数据")
    private BlankData blankData;

    @Schema(description = "答题数量")
    private Long answerCount = 0L;

    @Schema(description = "名次")
    private Integer rank = 1;

    @Schema(description = "分数统计")
    private PartySurveyPersonRecordEvaluateCount evaluateCount;

    /************非数据库字段 end **************/

    public void handleDefaultEvaluateCount() {
        this.evaluateCount = new PartySurveyPersonRecordEvaluateCount();
        PartySurveyPersonRecordEvaluateCount.EvaluateStatistics evaluateStatistics = new PartySurveyPersonRecordEvaluateCount.EvaluateStatistics();
        evaluateStatistics.setMinScore(0d);
        evaluateStatistics.setMaxScore(0d);
        evaluateStatistics.setAvgScore(0d);

        if(this.getLabelDataList()!=null){
            evaluateCount.setEvaluateCounts(this.getLabelDataList().stream().sorted(Comparator.comparing(LabelData::getScore)).map(labelData -> {
                PartySurveyPersonRecordEvaluateCount.EvaluateCount count = new PartySurveyPersonRecordEvaluateCount.EvaluateCount();
                count.setScore(labelData.getScore());
                count.setCount(0L);
                return count;
            }).collect(Collectors.toList()));
        }else{
            evaluateCount.setEvaluateCounts(Collections.emptyList());
        }

        evaluateCount.setEvaluateStatistics(evaluateStatistics);
    }
}
